package yc.star.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yc.star.common.constants.HttpConstants;
import yc.star.common.controller.BaseController;
import yc.star.common.domain.AppResult;
import yc.star.common.domain.vo.LoginUserVo;
import yc.star.system.domain.sysuser.dto.LoginDTO;
import yc.star.system.domain.sysuser.dto.SysUserSaveDTO;
import yc.star.system.domain.sysuser.vo.SysUserVO;
import yc.star.system.service.ISysUserService;

@RequestMapping("/sysUser")
@Tag(name = "管理员接口")
@RestController
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录",description = "根据账号密码进行登录")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "⽤⼾不存在")
    @ApiResponse(responseCode = "3103", description = "账号或密码错误")
    public AppResult<String> login (@RequestBody LoginDTO loginDTO) {
        String token = sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
        return AppResult.success(token);
    }

    @DeleteMapping("/logout")
    @Operation(summary = "退出登录")
    public AppResult<Void> logout (@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        boolean logout = sysUserService.logout(token);
        return toAppResult(logout);
    }

    @PostMapping("/add")
    @Operation(summary = "新增管理员", description = "根据提供的信息新增管理员⽤⼾")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾已存在")
    public AppResult<Void> add(@RequestBody SysUserSaveDTO saveDTO) {
        int rows = sysUserService.add(saveDTO);
        return toAppResult(rows);
    }

    @GetMapping("/info")
    @Operation(summary = "获取当前用户信息")
    public AppResult<LoginUserVo> info (@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return AppResult.success(sysUserService.info(token));
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除⽤⼾", description = "通过⽤⼾id删除⽤⼾")
    @Parameters(value = {@Parameter(name = "userId", in = ParameterIn.PATH, description = "⽤⼾ID")})
    @ApiResponse(responseCode = "1000", description = "成功删除⽤⼾")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public AppResult<Void> delete(@PathVariable Long userId) {
        return null;
    }

    @Operation(summary = "⽤⼾详情", description = "根据查询条件查询⽤⼾详情")
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description = "⽤⼾ID"),
            @Parameter(name = "sex", in = ParameterIn.QUERY, description = "⽤⼾性别")
    })
    @ApiResponse(responseCode = "1000", description = "成功获取⽤⼾信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public AppResult<SysUserVO> detail(Long userId, @RequestParam(required = false) String sex) {
        return null;
    }

}